package cn.kgc.crowdfundingadminspringboot.service;

import cn.kgc.crowdfundingadminspringboot.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadminspringboot.mapper.AdminMapper;
import cn.kgc.crowdfundingadminspringboot.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    AdminMapper adminMapper;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    /**
     * 登录
     * @param loginAcct
     * @param userPswd
     * @return 用户名密码争取返回Admin对象，否则返回null
     */
    @Override
    public Admin login(String loginAcct, String userPswd) {
        Admin admin=null;
        //连接数据库获取admin对象
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);

        List<Admin> list = adminMapper.selectByExample(adminExample);
        if (CrowdFundingUtils.collectionEffective(list)){
            admin=list.get(0);
            //对明文进行加密
            String md5userPswd =bCryptPasswordEncoder.encode(userPswd);
            //对比密文
            if (md5userPswd.equals(admin.getUserpswd())){
                return admin;
            }else{
                admin=null;
            }
        }

        return admin;
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Admin> pageInfo=null;

//        keyword不为空则按照条件查询
    if(CrowdFundingUtils.stringEffective(keyword)){
    AdminExample example=new AdminExample();
    AdminExample.Criteria criteria=example.createCriteria();
    criteria.andLoginacctLike("%"+keyword+"%");
        AdminExample.Criteria criteria2=example.createCriteria();
        criteria2.andLoginacctLike("%"+keyword+"%");
        AdminExample.Criteria criteria3=example.createCriteria();
        criteria3.andLoginacctLike("%"+keyword+"%");
        example.or(criteria2);
        example.or(criteria3);
        List<Admin> list=adminMapper.selectByExample(example);
        pageInfo=new PageInfo<>(list);
    }else{
        List<Admin> list=adminMapper.selectByExample(null);
        pageInfo=new PageInfo<>(list);
    }

        return pageInfo;
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);
        adminMapper.deleteByExample(example);
    }
    @Override
    public void saveAdmin(Admin admin) {
        String userPswd = admin.getUserpswd();
//        userPswd = CrowdFundingUtils.md5(userPswd);
        userPswd=bCryptPasswordEncoder.encode(userPswd);
        admin.setUserpswd(userPswd);

        // 执行保存
        adminMapper.insert(admin);
    }
    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }
    @Override
    public void updateAdmin(Admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String userPswd = admin.getUserpswd();
//            userPswd = CrowdFundingUtils.md5(userPswd);
            userPswd=bCryptPasswordEncoder.encode(userPswd);
            admin.setUserpswd(userPswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }

}
